package com.huwei.controller;


import com.huwei.controller.valid.PhoneValid;
import com.huwei.entity.UserAccount;
import com.huwei.servie.CodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.Errors;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/code")
public class CodeController {

    @Autowired
    private CodeService codeService;

    @GetMapping(value = "/imageCheckCode", produces = {"application/json; charset=UTF-8"})
    public String imageCheckCode(@RequestParam(name = "checkCodeWidth", required = false) Integer checkCodeWidth
            , @RequestParam(name = "checkCodeHeight", required = false) Integer checkCodeHeight) {
        checkCodeWidth = checkCodeWidth == null ? 200 : checkCodeWidth;
        checkCodeHeight = checkCodeHeight == null ? 80 : checkCodeHeight;
        return codeService.imageCheckCode(checkCodeWidth, checkCodeHeight);
    }

    /**
     * 请求短信验证码
     *
     * @return
     */
    @PostMapping(value = "/smsCode", produces = {"application/json; charset=UTF-8"})
    public String requestSmsCode(@Validated({PhoneValid.class}) UserAccount userAccount, Errors error) {
        return codeService.requestSmsCode(userAccount.getPhone());
    }
}
